package com.huike.controller;

import com.huike.po.*;
import com.huike.pojo.GroupCustomBean;
import com.huike.pojo.Response;
import com.huike.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
/**
 * 群相关操作请求控制处理
 */
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private GroupService groupService;
    @Autowired
    public Response response;

    @RequestMapping(value = "/chatRecord" )
    @ResponseBody
    public Response getChatRecord(String groupId, HttpSession session) {
        if (null != session.getAttribute("loginUser")) {
            response.setCode("400");
            response.setContent("请先登录");
        } else {
            List<GroupMessage> groupMessageList = groupService.getChatRecord(groupId);
            response.setCode("200");
            response.setContent(groupMessageList);
        }
        return response;
    }

    @RequestMapping(value = "/sendMessage" )
    @ResponseBody
    public Response sendMessage(@RequestBody GroupMessage message) {
        if (groupService.sendMessage(message))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/deleteOfflineMsg" )
    @ResponseBody
    public Response deleteOfflineMsg(String userAccount, String groupId) {
        if (groupService.deleteOfflineMsg(userAccount, groupId))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/groupMembers" )
    @ResponseBody
    public Response getGroupMembers(String groupId) {
        List<GroupMember> groupMemberList = groupService.getGroupMembersByGroupId(groupId);
        response.setCode("200");
        response.setContent(groupMemberList);
        return response;
    }

    @RequestMapping(value = "/createGroup")
    @ResponseBody
    /**
     * 创建群聊
     */
    public Response createGroup(@RequestBody GroupIdAndAccouts groupIdAndAccouts) {
        GroupDetail newGroup = groupService.createGroup(groupIdAndAccouts);
        if(newGroup!= null){
            response.setCode("200");
            response.setContent(newGroup);
        }
        else{
            response.setCode("500");
            response.setContent("");
        }
        return response;
    }

    @RequestMapping(value = "/agreeGroupInvite")
    @ResponseBody
    /**
     *  同意入群邀请
     */
    public Response agreeGroupInvite(@RequestBody GroupCustomBean groupRequest) {
        if(groupService.handleGroup(groupRequest,"agreeGroupInvite"))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/refuseGroupInvite")
    @ResponseBody
    /**
     * 拒绝入群邀请
     */
    public Response refuseGroupInvite(@RequestBody GroupCustomBean groupRequest) {
        if(groupService.handleGroup(groupRequest,"refuseGroupInvite"))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }



    @RequestMapping(value = "/inviteMember")
    @ResponseBody
    public Response inviteMember(@RequestBody GroupIdAndAccouts groupIdAndAccouts) {

//        String groupId = groupIdAndAccouts.getGroupId();
//        List<String> accounts = groupIdAndAccouts.getAccounts();

        if (groupService.inviteMember(groupIdAndAccouts))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/deleteMember")
    @ResponseBody
    /**
     * 删除群成员
     */
    public Response deleteMember(@RequestBody GroupIdAndAccouts groupIdAndAccouts) {
        if(groupService.deleteMember(groupIdAndAccouts))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/exitGroup")
    @ResponseBody
    /**
     * 退出群聊
     */
    public Response exitGroup(@RequestBody GroupWithOwnerAccount groupWithOwnerAccount) {
        if(groupService.exitGroup(groupWithOwnerAccount))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }




    @RequestMapping(value = "/GroupInvite")
    @ResponseBody
    /**
     *  发送入群邀请
     */
    public Response GroupInvite(@RequestBody GroupCustomBean groupRequest) {
        if(groupService.handleGroup(groupRequest,"GroupInvite"))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }



    @RequestMapping(value = "/applyGroup")
    @ResponseBody
    /**
     * 申请入群
     */
    public Response applyGroup(@RequestBody GroupCustomBean groupRequest) {
        if(groupService.handleGroup(groupRequest,"applyGroup"))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/agreeApplyGroup")
    @ResponseBody
    /**
     * 同意入群申请
     */
    public Response agreeApplyGroup(@RequestBody GroupCustomBean groupRequest) {
        if(groupService.handleGroup(groupRequest,"agreeApplyGroup"))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/refuseApplyGroup")
    @ResponseBody
    /**
     * 拒绝入群申请
     */
    public Response refuseApplyGroup(@RequestBody GroupCustomBean groupRequest) {
        if(groupService.handleGroup(groupRequest,"refuseApplyGroup"))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/sendFile" )
    @ResponseBody
    public Response sendFile(@RequestParam("fileContent") MultipartFile fileContent,
                             @RequestParam("msgFrom") String msgFrom,
                             @RequestParam("groupId") String groupId){
        GroupFileMsg message = new GroupFileMsg();
        message.setMsgFrom(msgFrom);
        message.setGroupId(groupId);
        message.setMsgContent(fileContent.getOriginalFilename());
//        System.out.println("???");
        if (groupService.sendFile(message, fileContent))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/getFileList" )
    @ResponseBody
    public Response getFileList(@RequestParam("groupId") String groupId){
        GroupFileMsg message = new GroupFileMsg();
        System.out.println("???");
        response.setCode("200");
        response.setContent(groupService.getFileList(groupId));
        return response;
    }


    @RequestMapping(value = "/getFile" )
    @ResponseBody
    public Response getFile(@RequestParam("groupId") String groupId, @RequestParam("fileName") String fileName){
        GroupFileMsg message = new GroupFileMsg();
        System.out.println("???");
        byte[] file = groupService.getFile(groupId, fileName);
        response.setCode("200");
        response.setContent(file);
        return response;
    }
}
